package giectech.spring.data;


import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import giectech.spring.data.mapper.CoffeeMapper;
import giectech.spring.data.model.Coffee;
import giectech.spring.data.service.CoffeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.List;

/**
 * @author Administrator
 * @title: SpringBucksApplication
 * @projectName springnewbucks
 * @description: TODO
 * @date 2022/8/1821:11
 */
@SpringBootApplication
@Slf4j
@MapperScan("gientech.spring.data.mapper")
@EnableTransactionManagement(mode = AdviceMode.PROXY)
public class SpringNewBucksApplication implements ApplicationRunner {

    @Autowired
    private CoffeeMapper coffeeMapper;

    @Autowired
    private CoffeeService coffeeService;

    @Autowired
    private StringRedisTemplate redisTemplate;


    public static void main(String[] args) {
        SpringApplication.run(SpringNewBucksApplication.class, args);
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {

        coffeeMapper.findAllWithRowBounds(new RowBounds(1,3)).forEach(c -> log.info("page(1) Coffee {}", c));

        coffeeMapper.findAllWithRowBounds(new RowBounds(1,3)).forEach(c -> log.info("page(2) coffee {}", c));

        List<Coffee> coffeeList = coffeeMapper.findAllWithParam(2,3);
        PageInfo page= new PageInfo(coffeeList);
        log.info("pageinfo: {}", page);

        log.info("===================");
        List<Coffee> coffeeList6 = coffeeService.findOneCoffeeByName("mocha");
        PageInfo page1= new PageInfo(coffeeList6);

        log.info("=======insert============");
        Coffee coffee = Coffee.builder().name("espresso")
                .price(Money.of(CurrencyUnit.of("CNY"), 20.0)).build();
        int result1 = coffeeService.save(coffee);
        List<Coffee> coffeeList1 = coffeeService.findOneCoffeeByName("espresso");
        log.info("save coffee result is {}", coffeeList1.get(0));

        log.info("======update=============");
        coffee.setName("newmocha");
        long result2 = coffeeService.updateByPrimaryKey(coffee);
        coffeeList1 = coffeeService.findOneCoffeeByName("newmocha");

        redisTemplate.opsForValue().set("coffeeList", coffeeList1.toString());
        String listStr =  (String)redisTemplate.opsForValue().get("coffeeList");

        List<Coffee> coffeeList2 = new Gson().fromJson(listStr, new TypeToken<List<Coffee>>() {}.getType());

        log.info("update coffee result is {}", coffeeList1.get(0));
        log.info("redis coffee result is {}", coffeeList2.get(0).getName());

        log.info("=====delete==============");
        int result3 = coffeeService.deleteByPrimaryKey(coffeeList1.get(0).getId());
        coffeeList1 = coffeeService.findOneCoffeeByName("newmocha");
        log.info("update coffee result is {}", coffeeList1.size() > 1 ? coffeeList1.get(0) : null );

    }
}
